﻿using MediatR;

namespace MediatRApp.Commands
{
    public class CreateStudentCommandResponse
    { 
    }
    public class CreateStudentCommand : IRequest<CreateStudentCommandResponse>
    {
        public string Name { get; set; }
    }
    public class CreateStudentCommandEvent : INotification
    {
        public string Name { get; set; }
    }

    public class MQCreateStudentCommandEventHander : INotificationHandler<CreateStudentCommandEvent>
    { 
        public Task Handle(CreateStudentCommandEvent notification, CancellationToken cancellationToken)
        {
            Console.WriteLine("已经发布MQ，给" + notification.Name);
            return Task.CompletedTask;
        }
    }
    public class EmailCreateStudentCommandEventHander : INotificationHandler<CreateStudentCommandEvent>
    {
        public Task Handle(CreateStudentCommandEvent notification, CancellationToken cancellationToken)
        {
            Console.WriteLine("已经发布Email，给" + notification.Name);
            return Task.CompletedTask;
        }
    }




    public class CreateStudentCommandHander : IRequestHandler<CreateStudentCommand, CreateStudentCommandResponse>
    {
        private readonly IMediator _mediator;
        public CreateStudentCommandHander(IMediator mediator)
        {
            _mediator = mediator;


        }
        public Task<CreateStudentCommandResponse> Handle(CreateStudentCommand request, CancellationToken cancellationToken)
        {

            Console.WriteLine("创建成功");

            _mediator.Publish(new CreateStudentCommandEvent() {Name = request.Name });
            return Task.FromResult(new CreateStudentCommandResponse());
        }
    }
}